<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Assignment_control extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url'));
		$this->load->library(array('form_validation','session'));
		$this->load->model('upload_model');
	}

	public function index()
	{
		if ( !$this->session->userdata('logged_in') ) {
		
			redirect('projekt/logged');
		
		}
		else
		{

		$config = array(
               array(
                     'field'   => 'name', 
                     'label'   => 'Názov', 
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'content', 
                     'label'   => 'Popis', 
                     'rules'   => 'required'
                  )
            );

		$this->form_validation->set_rules($config);

		if ( $this->form_validation->run() == FALSE )
		{

			$obrazky = array_diff(scandir('./attachments'), array('..', '.'));
			$this->load->view('assignment', array('error' => '', 'obrazky' => $obrazky));

		} else {

			$name = $this->input->post('name');
			$content = $this->input->post('content');
			$start = $this->input->post('start');
			$end = $this->input->post('end');

			$this->upload_model->new_mission($name, $content, $start, $end);

			redirect('projekt/logged');
			echo 'Zadanie pridané!';
		}
		}
	}

	function do_upload()
	{	

		if (!$this->session->userdata('logged_in')) {
		
			redirect('projekt/logged');
		
		}
		
		$config['upload_path'] = './attachments';
		$config['allowed_types'] = 'gif|jpg|png';

		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload() )
		{	
			$error = $this->upload->display_errors();

			$obrazky = array_diff(scandir('./attachments'), array('..', '.'));


			$this->load->view('assignment', array('error' => $error, 'obrazky' => $obrazky));
		}
		else
		{	
			$obrazok = $this->upload->data();

			chmod('./attachments/'.$obrazok['file_name'], 0755);

			$obrazky = array_diff(scandir('./attachments'), array('..', '.'));

			$this->load->view('assignment', array('error' => '', 'obrazky' => $obrazky));

			
		}
	}

	function delete_attachment($img)
	{
		unlink('./attachments/'.$img);
		$obrazky = array_diff(scandir('./attachments'), array('..', '.'));

		$this->load->view('assignment', array('error' => '', 'obrazky' => $obrazky));
	}


}
?>